
from django.utils.deprecation import MiddlewareMixin
from django.http import HttpResponseRedirect,JsonResponse,HttpResponse
import re

# rbac
WHITELIST=['/','/login','/logout','/admin/.*?','/index/','/api/.*?','/forbidden','/favicon.ico']

class RbacMiddleware(MiddlewareMixin):
    def process_request(self, request):
        url=str(request.path)
        for i in WHITELIST:
            re_s = "^{}$".format(i)
            if re.match(re_s, url):
                return None

        for i in request.session.get('PermissionList'):
            re_s = "^{}$".format(i)
            if re.match(re_s, url):
                return None
            else:
                return HttpResponseRedirect('/forbidden')


    def process_view(self, request, callback, callback_args, callback_kwargs):
        pass

    def process_response(self, request, response):
        return response